package com.qcxk.yuqian.system.utils;

import io.netty.channel.ChannelHandlerContext;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class DataUtils {

    public static String getCRC(String data) {
        data = data.replace(" ", "");
        int len = data.length();
        if (!(len % 2 == 0)) {
            return "0000";
        }
        int num = len / 2;
        byte[] para = new byte[num];
        for (int i = 0; i < num; i++) {
            int value = Integer.valueOf(data.substring(i * 2, 2 * (i + 1)), 16);
            para[i] = (byte) value;
        }
        return getCRC(para);
    }

    public static String getCRC(byte[] bytes) {
        //CRC寄存器全为1
        int CRC = 0x0000ffff;
        //多项式校验值
        int POLYNOMIAL = 0x0000a001;
        int i, j;
        for (i = 0; i < bytes.length; i++) {
            CRC ^= ((int) bytes[i] & 0x000000ff);
            for (j = 0; j < 8; j++) {
                if ((CRC & 0x00000001) != 0) {
                    CRC >>= 1;
                    CRC ^= POLYNOMIAL;
                } else {
                    CRC >>= 1;
                }
            }
        }
        //结果转换为16进制
        String result = Integer.toHexString(CRC).toUpperCase();
        if (result.length() != 4) {
            StringBuffer sb = new StringBuffer("0000");
            result = sb.replace(4 - result.length(), 4, result).toString();
        }
        //交换高低位
        return result.substring(2, 4) + result.substring(0, 2);
    }

    public static byte[] hexStringToBytes(String hexString) {
        if (hexString == null || hexString.equals("")) {
            return null;
        }
        hexString = hexString.toUpperCase();
        int length = hexString.length() / 2;
        char[] hexChars = hexString.toCharArray();
        byte[] d = new byte[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
        }
        return d;
    }

    public static String bytesToHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }

    private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }

    @SuppressWarnings("unchecked")
    public static String removeValue(Map hashMap, ChannelHandlerContext ctx) {
        Iterator<ConcurrentHashMap.Entry> iterator = (Iterator<ConcurrentHashMap.Entry>) hashMap.entrySet().iterator();
        while (iterator.hasNext()) {
            ConcurrentHashMap.Entry entry = iterator.next();
            ChannelHandlerContext o = (ChannelHandlerContext) entry.getValue();
            if (o.channel() == (ctx.channel())) {
                String key = (String) entry.getKey();
                hashMap.remove(key);
                return key;
            }
        }
        return null;
    }

    public static float decodeHEX(String hexs) {
//        BigInteger bigint=new BigInteger(hexs, 16);
//        int numb=bigint.intValue();
//        return numb;
        return Float.intBitsToFloat(new BigInteger(hexs, 16).intValue());
    }

    //    public static void main(String[] args) {
//        System.out.println(decodeHEX("EA57794B"));
//
//    }
    public static void main(String[] args) {
        String s = "82108194";
        Float value = Float.intBitsToFloat(new BigInteger(s, 16).intValue());
        System.out.println("16进制浮点数转10进制=" + new BigDecimal(value));
        System.out.println("10进制浮点数转16进制=" + Integer.toHexString(Float.floatToIntBits(value)));
    }

}
